﻿using LitJson;
using Mirror;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public struct ShipNetworkMessage : NetworkMessage
{
    public ShipNetworkMessage(ShipStruct ship, string path)
    {

        if(ship == null)
             ship = new ShipStruct();
        m_ShipStruct = ship;
        m_ShipPath = path;
    }

    public string m_ShipPath;
    public ShipStruct m_ShipStruct;
}

[AddComponentMenu("")]
public class PlayerNetwork : NetworkRoomPlayer
{
    Control m_Controle;
    [SyncVar]
    public Ship m_Ship;
    [SyncVar]
    public ShipNetworkMessage m_CurrentShipMessage;
    [SyncVar]
    public Vector3 m_CurrentArmPosition;
    [SyncVar]
    public int m_CurrentWeaponGroup;
    [SyncVar]
    public string m_PlayerName;
    [SyncVar]
    public bool m_IsDeath = false;

    public Tag m_Tag;
    public Tag m_TagPerfab;
    new void Start()
    {
        base.Start();
        SceneManager.sceneLoaded += OnSceneLoaded;
        CreateTag();
    }
    
    // Update is called once per frame
    void Update()
    {
        if (!UtilsGame.IsOnline())
            return;
        if (m_Ship)
        {
            m_Ship.arm(m_CurrentArmPosition, m_CurrentWeaponGroup);
        }
        if (isLocalPlayer)
        {
            if (Player.Instance.m_PlayerNetwork == null)
            {
                Player.Instance.m_PlayerNetwork = this;
                CmdSetPlayerName(Commander.Instance.m_Name);
            }
            if (m_Controle == null)
            {
                m_Controle = FindObjectOfType<Control>();
            }
            if (m_Controle)
            {
                if (m_Ship && m_Controle.ControlShp != m_Ship)
                {
                    m_Controle.ControlShp = m_Ship;
                    m_Controle.ControlShp.OnShipDestory.AddListener(OnShipDestroy);
                }
                else if(m_Controle.ControlShp == null)
                {
                    m_Controle.ControlShp = gameObject.GetComponent<Ship>();
                }
            }
            if( 0.1f < (m_CurrentArmPosition - m_Controle.m_FoucsPosition).magnitude &&
                NetworkClient.ready)
            {
                CmdSetArmPosition(m_Controle.m_FoucsPosition);
            }
            if(m_CurrentWeaponGroup != m_Controle.m_CurrentWeaponGroup &&
                NetworkClient.ready)
            {
                CmdSetWeaponGroup(m_Controle.m_CurrentWeaponGroup);
            }

            if(m_Ship != null && m_Ship != gameObject.GetComponent<Ship>())
            {
                gameObject.transform.position = m_Ship.transform.position;
            }
        }
        else
        {
            if(m_Tag && 
                m_Ship &&
                m_Tag &&
                m_Tag.m_BindObj != m_Ship)
            {
                m_Tag.m_BindObj = m_Ship.gameObject;
                m_Tag.m_NameL.text = m_PlayerName;
                m_Tag.m_NameL.text = m_PlayerName;
                m_Tag.gameObject.SetActive(true);
            }
        }
    }
    private void OnDestroy()
    {
        if(m_Tag)
            Destroy(m_Tag.gameObject);
    }
    void OnShipPathChanged(string _Old, string _New)
    {
    }
    #region Command
    [Command]
    public void CmdCurrentShipMessageChanged(ShipNetworkMessage value)
    {
        m_CurrentShipMessage = value;
        Debug.Log("Client Ready Ship is:" + value.m_ShipStruct.m_Name);
    }
    [Command]
    public void CmdSendPlayerMessage(string value)
    {
        RpcSendPlayerMessage(value);
    }
    [Command]
    public void CmdSetArmPosition(Vector3 value)
    {
        m_CurrentArmPosition = value;
    }
    [Command]
    public void CmdSetWeaponGroup(int value)
    {
        m_CurrentWeaponGroup = value;
    }
    [Command]
    public void CmdSetPlayerName(string value)
    {
        m_PlayerName = value;
    }
    [Command]
    public void CmdSetDeath(bool value)
    {
        m_IsDeath = true;
    }
    #endregion

    [ClientRpc]
    public void RpcSendPlayerMessage(string message)
    {
        MessageShow.Instance.Messaage(message);
    }
    public override void ReadyStateChanged(bool oldReadyState, bool newReadyState)
    {
        if(newReadyState == true && NetworkClient.ready)
        {
            var ship = Player.Instance.GetCurrentShip();
            if (ship == null)
            {
                return;
            }
            var message = ship.SaveToNetworkMessage();
            if (isLocalPlayer)
            {
                CmdCurrentShipMessageChanged(message);
            }
        }
    }

    public override void OnClientEnterRoom()
    {
        base.OnClientEnterRoom();
    }

    public override void OnClientExitRoom()
    {
        base.OnClientExitRoom();
    }

    void OnSceneLoaded(Scene scene, LoadSceneMode mode)
    {
        CreateTag();
    }

    void CreateTag()
    {
        if (!isLocalPlayer)
        {
            m_Tag = Instantiate(m_TagPerfab);
            var canvas = UtilsUnity.GetCanvas();
            if (canvas)
                m_Tag.transform.SetParent(canvas.transform);
        }
    }

    void OnShipDestroy(Ship ship)
    {
        CmdSetDeath(true);
    }
}
